package com.sb.registration.component.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utility class for performing validation on user data.
 * 
 * @author gautam.amitkumar@gmail.com
 *
 */
public class UserValidationUtil {
	
	public static final String EMAIL_PATTERN = "[[(\\S!#$%&'*+/=?^_`{|}~-]&&[^\"(),.;<>@\\[\\]^]]+(?:\\.[[\\S!#$%&'*+/=?^_`{|}~-]&&[^\"(),.;<>@\\[\\]^]]+)*(@){1}(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
	
	/**
	 * NOTE- THIS EMAIL VALIDATION PATTERN CAN BE LOADED FROM PROPERTIES FILE FOR EASY CONFIGURATION
	 */
	private static Pattern emailPattern = Pattern.compile(EMAIL_PATTERN, Pattern.CASE_INSENSITIVE);
	
	/**
	 * NOTE- THIS EMAIL VALIDATION PATTERN CAN BE LOADED FROM PROPERTIES FILE FOR EASY CONFIGURATION
	 */
	private static Pattern ipAddressPattern = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
	        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
	        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
	        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
	
	
	/**
	 * Map to hold the valid values for Channel field.
	 */
	private static List<String> validChannelsList = new ArrayList<String>();
	
	/**
	 * List to hold all valid email domains allowed for user registration. NOTE- FOR TIME BEING, I HAVE NOT CONSIDERED LOCALE OF USER,
	 * IRRESPECTIVE OF LOCALE, ALL PROVIDED DOMAINS WILL BE CONSIDERED VALID FOR THIS CODING CHALLENGE.
	 */
	private static List<String> validEmailDomains = new ArrayList<String>();
	
	/**
	 * Just for coding channel, the values has been hard-coded, it can be loaded from properties file for flexible configuration.
	 */
	static{
		validChannelsList.add("Mobile");
		validChannelsList.add("Web");
		validChannelsList.add("Application");
		validChannelsList.add("Customer Service");
		validChannelsList.add("Other");
		
		validEmailDomains.add(".org.uk");
		validEmailDomains.add("co.uk");
		validEmailDomains.add(".biz");
		validEmailDomains.add(".org.br");
		validEmailDomains.add(".info");
		validEmailDomains.add(".miapuesta-es,");
		validEmailDomains.add(".vistabet.ro");
	}
	
	
	/**
	 * Method to validate email address
	 * 
	 * @param email
	 * @return
	 */
	public static final boolean isValidEmail(String email) {

		Matcher m = emailPattern.matcher(email); 
		
		return m.matches();

	}
	
	/**
	 * Method to validate email address
	 * 
	 * NOTE - PATTERNS CAN BE USED HERE FOR VALIDATION IN FUTURE
	 * 
	 * @param email
	 * @return
	 */
	public static final boolean isValidEmailDomain(String email) {

		for(String validDomain : validEmailDomains){
			if(email.toLowerCase().endsWith(validDomain)){
				return true;
			}	
		}
		
		return false;

	}
	
	/**
	 * Method to validate channel
	 * 
	 * NOTE - PATTERNS CAN BE USED HERE FOR VALIDATION IN FUTURE
	 * 
	 * @param channel
	 * @return
	 */
	public static final boolean isValidChannel(String channel){
		
		return validChannelsList.contains(channel);
		
	}
	
	/**
	 * Method to validate IP Address
	 * 
	 * @param ipAddress
	 * @return
	 */
	public final static boolean validateIPAddress(String ipAddress) {
		
		Matcher m = ipAddressPattern.matcher(ipAddress); 
		
		return m.matches();
	}

}
